package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcAdministrative;
import com.csun.cmny.provider.model.domain.OmcNursingRecords;
import com.csun.cmny.provider.model.dto.AdministrativeSaveDto;
import com.csun.cmny.provider.model.dto.nursingRecords.NursingRecordsQueryDto;
import com.csun.cmny.provider.model.dto.nursingRecords.NursingRecordsSaveDto;
import com.csun.cmny.provider.model.dto.nursingRecords.NursingRecordsUpdateDto;
import com.csun.cmny.provider.model.vo.nursingRecords.NursingRecordsVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcNursingRecordsService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/nursingRecords")
public class OmcNursingRecordsController extends BaseController {

    @Resource
    private OmcNursingRecordsService nursingRecordsService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增护理项目记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid NursingRecordsSaveDto nursingRecordsSaveDto, BindingResult br) {

        OmcNursingRecords omcNursingRecords = new OmcNursingRecords();
        BeanUtils.copyProperties(nursingRecordsSaveDto,omcNursingRecords);
        omcNursingRecords.setOperator(getLoginAuthDto().getUserName());
        omcNursingRecords.setGroupId(getLoginAuthDto().getGroupId());
        omcNursingRecords.setUserId(getLoginAuthDto().getUserId());
        int save = nursingRecordsService.save(omcNursingRecords);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除护理项目记录")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper delete(Long id) {

        int delete = nursingRecordsService.deleteByKey(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改护理项目记录")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid NursingRecordsUpdateDto nursingRecordsUpdateDto, BindingResult br) {

        OmcNursingRecords omcNursingRecords = new OmcNursingRecords();
        BeanUtils.copyProperties(nursingRecordsUpdateDto,omcNursingRecords);
        int update = nursingRecordsService.update(omcNursingRecords);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询老人护理项目记录")
    @RequestMapping(value = "/selectPageQueryByOld", method = RequestMethod.POST)
    public Wrapper swlecw(@RequestBody @Valid NursingRecordsQueryDto nursingRecordsQueryDto) {

        List<NursingRecordsVo>list= nursingRecordsService.selectPageQueryByOld(nursingRecordsQueryDto);
        for (NursingRecordsVo nursingRecordsVo : list) {
            nursingRecordsVo.setMoney(nursingRecordsVo.getUnitPrice()*nursingRecordsVo.getQuantity());
        }
        PageInfo<NursingRecordsVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
